<?php
	class TheKhuyenMai extends CI_Controller
	{
		function admin_index()
		{
			$login = $this->session->userdata('islogin');
			if($login)
			{	
				$this->load->model('thekhuyenmai_model');
				$data['thekhuyenmai'] = $this->thekhuyenmai_model->LayTatCaTheKhuyenMai();
				
				$data['key'] = NULL;
				
				$data['chucvu'] = $this->session->userdata('chucvu');
				
				$data['current_username'] = $this->session->userdata('username');
			
				$this->load->view('admin/thekhuyenmai_view', $data);
			}
			else
				redirect('login/admin_index');
		}
		
		function Tim()
		{
			$login = $this->session->userdata('islogin');
			if($login)
			{
				$key = $this->input->post('key');
				if(!$key)
					$key = urldecode($this->uri->segment(3));
					
				$this->load->model('thekhuyenmai_model');
				$data['thekhuyenmai'] = $this->thekhuyenmai_model->Tim($key);
				
				$data['key'] = $key;
				
				$data['chucvu'] = $this->session->userdata('chucvu');
				
				$data['current_username'] = $this->session->userdata('username');

				$this->load->view('admin/thekhuyenmai_view', $data);
			}
			else
				redirect('login/admin_index');			
		}
		
		function LayTheKhuyenMai_ajax()
		{
			$login = $this->session->userdata('islogin');
			if($login)
			{	
				$key = $this->input->post('name_startsWith');
				
				$this->load->model('thekhuyenmai_model');
				$data['sothe'] = $this->thekhuyenmai_model->LaySoThe_ajax($key);
				
				$this->load->view('admin/ajax_danhsachthekhuyenmai', $data);
			}
			else
				redirect('login/admin_index');			
		}
		
		function Reset()
		{
			$login = $this->session->userdata('islogin');
			if($login)
			{
				echo "<meta http-equiv='Content-Type' content='text/html; charset=utf-8' />";
				
				$this->load->model('thekhuyenmai_model');
				$result = $this->thekhuyenmai_model->Reset();
								
				if(!$result)
				{
					?>
                    <script>
						alert('Reset thẻ khuyến mãi thất bại!');
						window.location = "<?php echo base_url(); ?>index.php/thekhuyenmai/admin_index";						
					</script>
                    <?php
				}
				
				?>
                <script>
					alert('Các thẻ khuyến mãi đã được reset!');
					window.location = "<?php echo base_url(); ?>index.php/thekhuyenmai/admin_index";						
				</script>
                <?php				
			}
			else
				redirect('login/admin_index');			
		}
		
		function Xoa()
		{
			$login = $this->session->userdata('islogin');
			if($login)
			{			
				$sothe = $this->uri->segment(3);
				if($sothe)
				{
					$this->load->model('thekhuyenmai_model');
					$result = $this->thekhuyenmai_model->Xoa($sothe);
					if($result)
					{
						echo "<meta http-equiv='Content-Type' content='text/html; charset=utf-8' />";
						?>
	            	    <script>
							alert("Thẻ khuyến mãi đã được xóa!");
							window.location = "<?php echo base_url(); ?>index.php/thekhuyenmai/admin_index";
						</script>
	                	<?php
					}
					else
					{
						echo "<meta http-equiv='Content-Type' content='text/html; charset=utf-8' />";
						?>
	    	            <script>
							alert("Xóa thẻ khuyến mãi thất bại!");
							window.location = "<?php echo base_url(); ?>index.php/thekhuyenmai/admin_index";
						</script>
	    	            <?php
					}			
				}
			}
			else
				redirect('login/admin_index');			
		}
		
		function Them()
		{
			$login = $this->session->userdata('islogin');
			if($login)
			{	
				$soluong = $this->input->post('soluong');
				
				for($i = 0 ; $i < $soluong ; $i++)
				{
					if($i<10)
						$ma = 'GYEBH'.$i;
					else
						if($i>=10 && $i<100)
							$ma = 'ALOP'.$i;
						else
							if($i>=100 && $i<1000)
								$ma = 'HKR'.$i;
							else
								if($i>=1000 && $i<10000)
									$ma = 'VF'.$i;
								else
									if($i>=100000 && $i<1000000)
										$ma = 'S'.$i;
								
					$timezone = "Asia/Ho_Chi_Minh";
					if(function_exists('date_default_timezone_set')) 
						date_default_timezone_set($timezone);
					
					$sothe = date('dYmHis').$ma;
					
					$this->load->model('thekhuyenmai_model');
					$result = $this->thekhuyenmai_model->Them($sothe);
					
					if($result == 0)
					{
						echo "<meta http-equiv='Content-Type' content='text/html; charset=utf-8' />";
						?>
	                    <script>
							alert('Thêm thẻ khuyến mãi thất bại!');
							window.location = "<?php echo base_url(); ?>index.php/thekhuyenmai/admin_index";						
						</script>
                	    <?php
						return;
					}				
				}
				
				echo "<meta http-equiv='Content-Type' content='text/html; charset=utf-8' />";
				?>
	            <script>
					alert('Thẻ khuyến mãi đã được thêm!');
					window.location = "<?php echo base_url(); ?>index.php/thekhuyenmai/admin_index";						
				</script>
          	    <?php				
			}
			else
				redirect('login/admin_index');			
		}
	}
?>